<?php

namespace App\Http\Controllers\Api;

use App\Common\Libs\ArrayHelper;
use App\Consts\GlobalConst;
use App\Exceptions\Error;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;

class ArticleController extends BaseController
{
    public $validate = [
        "getDetail" => ["rules" => [
            "id" => "required|string|max:24",
        ]],
        'similarList' => [
            'rules' => [
                'id' => 'required|string|max:24',
            ]
        ]
    ];

    /**
     * @path /article/get-detail
     * @category 文章
     * @title 【有备注】详情
     * @method POST
     * @params [{"name":"id","type":"text","required":1,"example":"","desc":"文章ID"}]
     * @resBody {"success":true,"statusCode":200,"message":"操作成功","result":{"id":"文章ID","author_id":"作者ID","cover_url":"文章封面图，如1.jpg","title":"文章标题","description":"文章描述","type":"类型，1推荐，1原创","is_deleted":0,"is_shield":0,"created_at":"创建时间戳","content":"文章内容","is_collect":0,"is_recommend":0,"author":{"id":"用户ID","nickname":"用户昵称","avatar":"用户头像，如1.jpg","special_signature":"用户个性签名","attention_status":"关注关系，0没关系，1关注，2粉丝，3互关"},"note_list":[{"id":"24位笔记ID","start_index":"1","end_index":"2"}],"original":{"author_name":"作者名称","account_name":"来源名称","from":"来源名","time":"来源创建时间戳","url":"来源地址"},"recommend":["1.jpg"],"count":{"collect":"收藏数","note":"笔记数","recommend":"推荐数"}}}
     * @description 1.【前端】落地页详情也使用此接口
     */
    public function getDetail(Request $request)
    {
        $articleId = $request->post('id');

        // 文章基础信息
        $article = service()->ArticleService->getArticleByArticleId($articleId);
        if (!$article) {
            return $this->apiReturn(Error::handle());
        }

        // 文章拓展信息
        $articleMore = service()->ArticleService->getArticleMoreFields($article, $this->getCurrentUserId());

        // 推送到推荐系统
        service()->RecommendSystemService->addView([
            'target_id' => $article->article_id,
            'target_type' => $article->type,
            'user_id' => $this->getCurrentUserId(),
        ]);
        return $this->apiReturn(ArrayHelper::merge($article->commonFormat(), $articleMore));
    }

    /**
     * @path /article/similar
     * @category 文章
     * @title 相关文章
     * @method POST
     * @params [{"name":"id","type":"text","required":1,"example":"","desc":"当前查看文章ID"}]
     * @resBody {"success":true,"statusCode":200,"message":"操作成功","result":{"have_more":true,"list":[{"id":"文章ID","title":"文章标题","cover_url":"文章封面图，如1.jpg"}]}}
     */
    public function similarList(Request $request)
    {
        $params = $request->post();

        $similarList = service()->ArticleService->getSimilarArticleList(ArrayHelper::merge($this->getBaseInfo($params), [
            'page_size' => GlobalConst::PAGE_COUNT_EIGHT,
            'ignore' => $params['id'],
        ]));

        return $this->apiReturn($similarList);
    }
}
